package com.ztq.test.login.service;

import java.util.Date;

import com.ztq.test.login.dao.LoginDao;
import com.ztq.test.login.entity.T_userInfo;
import com.ztq.test.system.util.CommonUtils;

/**
 * @author: ztq
 * @date: 2016-1-8 下午03:29:36
 * @description: 处理用户登录注册的service实现类
 * @version:1.0
 */
public class LoginServiceIMPL implements LoginService {
	private LoginDao loginDao;
	
	public LoginDao getLoginDao() {
		return loginDao;
	}

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}
	
	// 判断state为1的用户名username是否存在
	public boolean judgeUsernameExist(T_userInfo userInfo){
		boolean flag = false;
		if(loginDao.queryUsernameRecord(userInfo) != null){
			flag = true;
		}
		return flag;
	}
	
	// 判断state为1的邮箱email是否存在
	public boolean judgeEmailExist(T_userInfo userInfo){
		boolean flag = false;
		if(loginDao.queryEmailRecord(userInfo) != null){
			flag = true;
		}
		return flag;
	}

	// 用户注册逻辑
	public boolean register(T_userInfo userInfo) {
		userInfo.setUser_id(CommonUtils.getUUID());
		userInfo.setRegister_time(new Date());
		userInfo.setState(1);    // 1表示正常状态，4表示注销状态
		userInfo.setPassword(CommonUtils.ToMD5(userInfo.getPassword()));   // 密码md5加密
		return loginDao.insert(userInfo);
	}
	
	//判断用户名、密码是否都正确
	public boolean judgePasswordExist(T_userInfo userInfo){
		boolean flag = false;
		userInfo.setPassword(CommonUtils.ToMD5(userInfo.getPassword()));   // 密码md5加密
		if(loginDao.queryPasswordRecord(userInfo) != null){
			flag = true;
		}
		return flag;
	}
	
	//得到用户的id
	public String getUserId(T_userInfo userInfo){
		return loginDao.queryUserId(userInfo);
	}
}